package cn.jhc.teachingquality.net.practice;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;

import org.jsoup.nodes.Document;

import java.util.HashMap;
import java.util.Map;

import cn.jhc.teachingquality.Global;
import cn.jhc.teachingquality.bean.PracticeDetail;
import cn.jhc.teachingquality.net.TeachingQualityRequest;

/**
 * Created by luyanfei on 16-3-17.
 */
public class PracticeDetailRequest extends TeachingQualityRequest<PracticeDetail> {
    private static final String DETAIL_PRACTICE_URL = Global.URL_PREFIX + "/dgSxrzAction!detail.shtml";
    private String hiddenId;

    public PracticeDetailRequest(String hiddenId, Response.Listener<PracticeDetail> listener) {
        super(Method.POST, DETAIL_PRACTICE_URL, listener);
        this.hiddenId = hiddenId;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        Map<String,String> map = new HashMap<>();
        map.put("id", hiddenId);
        map.put("dgSxrz.id", hiddenId);
        return map;
    }

    @Override
    protected PracticeDetail handleResponse(Document document) {
        String detailHiddenId;
        String content;
        String remark;
        try {
            detailHiddenId = document.getElementsByAttributeValue("name","dgSxrz.dgSxqk.id").first().val().trim();
            content = document.getElementsByAttributeValue("name", "dgSxrz.nr").first().text();
            remark = document.getElementsByAttributeValue("name","dgSxrz.jsYj").first().text();
        } catch (NullPointerException e) {
            Log.w(Global.LOG_TAG, "Cannot extract practice log details, null returned.");
            return null;
        }
        return new PracticeDetail(content, detailHiddenId, remark);
    }
}
